package com.woniuxy.ticketorder.controller;

import com.woniuxy.ticketcommon.ResponceResult;
import com.woniuxy.ticketorder.entity.dto.ScDTO;

import com.woniuxy.ticketorder.entity.dto.SeatDTO;
import com.woniuxy.ticketorder.service.CinemaService;
import com.woniuxy.ticketorder.service.Ticket_orderService;
import com.woniuxy.ticketorder.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author order
 * @since 2021-02-22
 */
@Api(tags = "订单管理接口")
@Slf4j
@RestController
@RequestMapping("/order")
public class Ticket_orderController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserService userService;

    private Ticket_orderService orderService;

    private CinemaService cinemaService;

    @ApiOperation("调用用户微服务的方法1")
    @GetMapping("add")
    public String add() {

        log.debug("order Add");

        String serviceUser = "http://ticket-user/user/add";
//
//        try {
//            URL url=new URL(serviceUser);
//            URLConnection urlConnection=url.openConnection();
//            InputStream inputStream=urlConnection.getInputStream();
//
//            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
//
//            log.debug(reader.readLine());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        ResponseEntity<String> forEntity = restTemplate.getForObject(serviceUser, String.class);
        String forObject = restTemplate.getForObject(serviceUser, String.class);
        System.out.println(forObject);
        return "order Add";
    }



    @ApiOperation("添加用户的接口")
    //（{}）里面可能是数组
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    }
    )
    @GetMapping("add2")
    public String add2() {


        return userService.add();
    }

    @ApiOperation("下订单的接口")
    @PostMapping("orderDown")
    public ResponceResult<Object> orderDown(int[] ids, int hall_id, HttpSession session) {

        int num=0;

        int userid=(Integer)session.getAttribute("userid");

        String seat="";
        //传回座位信息及购买数量
        for (int id : ids) {
            SeatDTO seatDTO=cinemaService.findDtoById(id);
            //判断座位是否被占
            seat=seat+seatDTO.getRowno()+"排"+seatDTO.getColno()+"号  ";

            if (cinemaService.checkStateBySeatId(id)){
                return new ResponceResult<>(412,"座位已被买");
            }
            //修改座位信息
            cinemaService.changeStateBySeatId(id);

            num++;
        }
        //传影厅信息时间，价格
        ScDTO scDTO=cinemaService.findscByHallId(hall_id);

        //添加订单
        orderService.addOrder(scDTO.getName(),seat,scDTO.getCinema_name(),scDTO.getPrice(),num,userid);
        //添加用户积分
        userService.addSocre(userid);

        return new ResponceResult<>(200, "下单成功");
    }


}

